import {
  // 预警劝阻概览
  getDissuadedOverview,
  // 新增预警趋势
  getWarningTrend,
  // 预警推送趋势
  getWarningPushTrend,
  // 劝阻状态占比
  getDissuadeStateProportion,
  // 处置方式占比
  getDisposalProportion,
  // 反馈情况占比
  getFeedbackProportion,
  // 反馈情况趋势
  getFeedbackTrend,
  // 劝阻状态趋势
  getDissuadeStateTrend,
  // 处置方式趋势
  getDisposalTrend,
  // 未接原因趋势
  getMissedCallTrend,
  // 止损金额概况趋势
  getStopLossAmountTrend,
  // 预警反诈类型占比
  getFraudTypeRatio,
  // 预警诈骗等级占比
  getFraudLevelRatio,
  // 预警数据来源占比
  warningDataSourceRatio,
  // 案件统计
  caseScreen,
} from '@/views/daping/api/index.js'

// 统一配置成{name: xxx, value: xxx}格式
function setChartData(list, nameKey, valueKey) {
  list.map(item => {
    item.name = item[nameKey],
    item.value = item[valueKey] || 0
    // data.push({
    //   name: item[nameKey],
    //   value: item[valueKey] || 0
    // })
  })
  return list
}

function setGroupPieData(list, nameKey, valueKey, childrenKey) {
  let data = []
  
  list.map(item => {
    
    let obj = {
      name: item[nameKey],
      value: item[valueKey] || 0,
      children: []
    }
    item[childrenKey].map(child => {
      let value = (child[valueKey] / item[valueKey]) * 100
      if (value % 1 !== 0) {
        value = Number(value.toFixed(2))
      }
      obj.children.push({
        name: child[nameKey],
        value: value
      })
    })
    data.push(obj)
  })
  
  return data
}

function setChartLineData(list, nameKey, valueKey) {
  let obj = {}
  list.map(item => {
    item.points.map(child => {
      obj[child.typeName] = []
    })
  })
  list.map(item => {
    item.points.map(child => {
      obj[child.typeName].push({
        name: child[nameKey],
        value: child[valueKey] || 0
      })
    })
  })
  return obj
}

// 预警劝阻概览
async function _getDissuadedOverview(params) {
  const query = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await getDissuadedOverview(query)
  return result
}
// 案件统计
async function _caseScreen(params) {
  const query = {
    caseTimeType: params.dateType,
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    startTimeFrame: params.startDate,
    stopTimeFrame: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await caseScreen(query)
  return result
}
// 新增预警趋势
async function _getWarningTrend(params) {
  // dateType: 2, // 0:自定义 1：昨天 2：今天 3：近三天 4：近七天 5：近一个月 6：近一个季度 7：近一年	query	false
  const query = {
    dateTimeType: '',
    endTime: '',
    startTime: '',
    xzqh: params.xzqh
  }
  switch (params.dateType) {
    case 0:
      query.dateTimeType = 'CUSTOMIZE'
      query.startTime = params.startDate
      query.endTime = params.endDate
      break;
    case 2:
      query.dateTimeType = 'TODAY'
      break;
    case 4:
      query.dateTimeType = 'SEVEN_DAY'
      break;
    case 5:
      query.dateTimeType = 'ONE_MONTH'
      break;
  }
  const {
    result
  } = await getWarningTrend(query)
  return setChartData(result, 'date', 'count')
}
// 预警推送/劝阻状态趋势
async function _getPushAndDiss(params) {
  const query = {
    dateTimeType: '',
    endTime: '',
    startTime: '',
    xzqh: params.xzqh
  }

  switch (params.dateType) {
    case 0:
      query.dateTimeType = 'CUSTOMIZE'
      query.startTime = params.startDate
      query.endTime = params.endDate
      break;
    case 2:
      query.dateTimeType = 'TODAY'
      break;
    case 4:
      query.dateTimeType = 'SEVEN_DAY'
      break;
    case 5:
      query.dateTimeType = 'ONE_MONTH'
      break;
  }
  const stateQuery = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const pushRes = await getWarningPushTrend(query)
  const stateRes = await getDissuadeStateTrend(stateQuery)
  let obj = {}
  // 合并数组
  if (pushRes.result && pushRes.result.length) {
    let list = []
    pushRes.result.map(item => {
      list.push({
        name: item.date,
        value: item.count || 0
      })
    })
    obj['预警推送'] = list
  }
  if (stateRes.result && stateRes.result.length) {
    // 设置key
    stateRes.result.map(item => {
      item.points.map(child => {
        obj[child.typeName] = []
      })
    })
    // 填充数据
    stateRes.result.map(item => {
      item.points.map(child => {
        obj[child.typeName].push({
          name: child.date,
          value: child.count || 0
        })
      })
    })
  }
  
  
  return obj
}

// 处置方式占比
async function _getDisposalProportion(params) {
  const query = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await getDisposalProportion(query)
  result.map(item => {
    item.value = item.count
    item.name = item.product
  })
  return result
}
// 劝阻状态占比
async function _getDissuadeStateProportion(params) {
  const query = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await getDissuadeStateProportion(query)
  let obj = {
    
  }
  result.map(item => {
    obj[item.product] = {
      children: [],
      total: item.count
    }
    if(item.children && item.children.length) {
      item.children.map(child => {
        obj[item.product].children.push({
          name: child.product,
          value: child.count
        })
      })
    }
  })
  // const data = setGroupPieData(result, 'product', 'count', 'children')
  return obj
}
// 反馈情况占比
async function _getFeedbackProportion(params) {
  const query = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await getFeedbackProportion(query)
  let obj = {
    
  }
  result.map(item => {
    obj[item.product] = {
      children: [],
      total: item.count
    }
    if(item.children && item.children.length) {
      item.children.map(child => {
        obj[item.product].children.push({
          name: child.product,
          value: child.count
        })
      })
    }
  })
  return obj
}
// 劝阻结果趋势
async function _getFeedbackTrend(params) {
  const query = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await getFeedbackTrend(query)
  let obj = {

  }
  if (result.length > 0) {
    const isContat = result[0]
    const unContat = result[1]
    isContat.map(item => {
      item.points.map(child => {
        obj[child.typeName] = []
      })
    })
    isContat.map(item => {
      item.points.map(a => {
        unContat.map(child => {
          child.points.map(c => {
            if ((c.date === a.date) && (c.typeName === a.typeName)) {
              obj[c.typeName].push({
                name: c.date,
                value: a.count + c.count
              })
            }
          })
        })
      })
    })
  }


  return obj
}
// 处置方式趋势
async function _getDisposalTrend(params) {
  // dateType: 2, // 0:自定义 1：昨天 2：今天 3：近三天 4：近七天 5：近一个月 6：近一个季度 7：近一年	query	false
  const query = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await getDisposalTrend(query)
  return setChartLineData(result, 'date', 'count')
}





// 预警反诈类型占比
async function _getFraudTypeRatio(params) {
  const query = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await getFraudTypeRatio(query)
  const data = setChartData(result, 'product', 'ratio')
  let list = []
  let other = {
    name: '其他',
    value: 0
  }
  
  data.map((item, index) => {
    let value = item.value.split('%')[0] || 0
    if (index < 5) {
      item.value = Number(value)
      list.push(item)
    } else {
      other.value += Number(value)
    }
  })
  if (other.value % 1 !== 0) {
    other.value = Number(other.value.toFixed(2))
  }
  if (data.length > 0) {
    other.ratio = other.value + '%'
    list.push(other)
  }
  return list
}
// 预警数据来源占比
async function _warningDataSourceRatio(params) {
  const query = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await warningDataSourceRatio(query)
  const data = setChartData(result, 'product', 'ratio')
  data.map(item => {
    let value;
    if (item.value) {
      value = item.value.split('%')[0] || 0
      value = Number(value)
    } else {
      value = 0
    }
    item.value = value
  })
  return data
}

// 未接原因趋势
async function _getMissedCallTrend(params) {
  // dateType: 2, // 0:自定义 1：昨天 2：今天 3：近三天 4：近七天 5：近一个月 6：近一个季度 7：近一年	query	false
  const query = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await getMissedCallTrend(query)
  return setChartLineData(result, 'date', 'count')
}
// 止损金额概况趋势
async function _getStopLossAmountTrend(params) {
  // dateType: 2, // 0:自定义 1：昨天 2：今天 3：近三天 4：近七天 5：近一个月 6：近一个季度 7：近一年	query	false
  const query = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await getStopLossAmountTrend(query)
  return setChartLineData(result, 'date', 'count')
}
// 预警诈骗等级占比
async function _getFraudLevelRatio(params) {
  const query = {
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await getFraudLevelRatio(query)
  const data = setChartData(result, 'product', 'ratio')
  data.map(item => {
    let value = item.value.split('%')[0] || 0
    item.value = Number(value)
  })
  return data
}


// 案件数、嫌疑人数量变化趋势
async function _caseAreaQuery(params) {
  // dateType: 2, // 0:自定义 1：昨天 2：今天 3：近三天 4：近七天 5：近一个月 6：近一个季度 7：近一年	query	false
  const query = {
    caseTimeType: params.dateType,
    warningTimeType: params.dateType,
    warningStartTime: params.startDate,
    warningEndTime: params.endDate,
    startTimeFrame: params.startDate,
    stopTimeFrame: params.endDate,
    xzqh: params.xzqh
  }
  const {
    result
  } = await caseScreen(query)
  let obj = {
    '案件数': [],
    '嫌疑人数': []
  }
  if(result) {
    if(result.changeTrendOfTheNumberOfCases && result.changeTrendOfTheNumberOfCases.length) {
      let caseObj = setChartLineData(result.changeTrendOfTheNumberOfCases, 'date', 'count')
      Object.keys(caseObj).forEach(key => {
        obj['案件数'].push(caseObj[key][0])
      })
    }
    
    if(result.changeTrendOfTheNumberOfSuspects && result.changeTrendOfTheNumberOfSuspects.length) {
      let caseObj = setChartLineData(result.changeTrendOfTheNumberOfSuspects, 'date', 'count')
      Object.keys(caseObj).forEach(key => {
        obj['嫌疑人数'].push(caseObj[key][0])
      })
    }
  }
  return obj
}


export default {
  '预警劝阻概览': _getDissuadedOverview,
  '案件统计': _caseScreen,
  '新增预警趋势（疑似受害人）': _getWarningTrend,
  '预警推送/劝阻状态趋势': _getPushAndDiss,
  '劝阻状态占比': _getDissuadeStateProportion,
  '处置方式占比': _getDisposalProportion,
  '反馈情况占比': _getFeedbackProportion,
  '劝阻结果趋势': _getFeedbackTrend,
  '未接原因趋势': _getMissedCallTrend,
  '止损金额概况趋势': _getStopLossAmountTrend,
  '预警反诈类型占比': _getFraudTypeRatio,
  '预警诈骗等级占比': _getFraudLevelRatio,
  '预警数据来源占比': _warningDataSourceRatio,
  '处置方式趋势': _getDisposalTrend,
  '案件数、嫌疑人数量变化趋势': _caseAreaQuery
}
